﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading; 
using VipReminder.ViewModel;

namespace VipReminder.Views
{
    /// <summary>
    /// WorkBreak.xaml 的交互逻辑
    /// </summary>
    public partial class WorkBreak
    {

        // 定时器
        private DispatcherTimer _timer;

        public VmReminder _vm;



        public WorkBreak()
        {
            InitializeComponent();
            WindowState = WindowState.Maximized; //全屏显示
            ShowInTaskbar = false;   //任务栏不显示图标 
            Topmost = true; //窗体最前端显示
            //this.countDown = countDown; == TODO CountDown 单例
        }

        private void WorkBreak_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)//Esc键  
            {
                OpenCountDown();
            }
        }

        private void OpenCountDown()
        {
            CountDown countDown = new CountDown();
            Application.Current.MainWindow = countDown;
            countDown.DataContext = this._vm;
            countDown.InitData();
            countDown.Show();
            _timer.Stop();
            this.Close();
        }


        public void InitData()
        {
            _vm = DataContext as VmReminder;

            this.ShowInTaskbar = false;  //任务栏不显示图标

            _vm.CountDownBreakMinute = _vm.BreakValue;
            _vm.CountDownBreakSecond = 0;

            _timer = new DispatcherTimer();
            _timer.Tick += Timer_OnTick;
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Start();

            //KeyboardBlocker.off();//锁键盘
        }


        private void Timer_OnTick(object sender, EventArgs e)
        {
            //倒计时时结束，弹出休息窗口
            if (_vm.CountDownBreakMinute == 0 && _vm.CountDownBreakSecond == 0)
            {
                _timer.Stop();
                OpenCountDown();
                return;
            }
            //如果分钟大于0，秒=0，那分-1，秒>0 减秒
            if (_vm.CountDownBreakMinute > 0)
            {
                if (_vm.CountDownBreakSecond > 0)
                {
                    _vm.CountDownBreakSecond--;
                }
                else
                {
                    _vm.CountDownBreakMinute--;
                    _vm.CountDownBreakSecond = 60;
                }
            }

            if (_vm.CountDownBreakMinute == 0)
            {
                _vm.CountDownBreakSecond--;
            }
        }

    }
}
